package com.wcar.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.FrontUser;
import com.wcar.common.entity.FrontUserStore;
import com.wcar.common.vo.Result;
import com.wcar.store.dao.FrontUserDao;
import com.wcar.store.dao.FrontUserStoreDao;
import com.wcar.store.service.FrontUserService;
import com.wcar.store.until.SendCodeUtil;
import com.wcar.store.vo.AddUserVo;
import com.wcar.store.vo.FrontUserStoreVO;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @BelongsProject: four-wcar-idea
 * @BelongsPackage: com.wcar.store.service.impl
 * @Author: 李睢康
 * @CreateTime: 2024-06-03  14:59
 */
@Service
public class FrontUserServiceImpl implements FrontUserService {
    @Resource
    private FrontUserDao frontUserDao;
    @Resource
    private FrontUserStoreDao frontUserStoreDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<FrontUserStore> findFrontUser(Integer storeId, Integer userId) {
        QueryWrapper<FrontUserStore> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("store_id",storeId);
        FrontUserStore frontUserStore = frontUserStoreDao.selectOne(wrapper);
        return new Result<>(200,"查询用户绑定信息成功",frontUserStore);
    }

    @Override
    public Result<Integer> insert(FrontUserStoreVO frontUserStoreVO) {
        FrontUserStore frontUserStore = new FrontUserStore();
        frontUserStore.setUserId(frontUserStoreVO.getUserId());
        frontUserStore.setStoreId(frontUserStoreVO.getStoreId());
        int insert = frontUserStoreDao.insert(frontUserStore);
        return new Result<>(200,"绑定店铺成功",insert);
    }

    @Override
    public Result<String> selectPhone(String phone) {
        QueryWrapper<FrontUser> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",phone);
        FrontUser frontUser = frontUserDao.selectOne(wrapper);
        if (frontUser != null) {
            return new Result<>(500,"此手机号已被注册","此手机号已被注册");
        }
        return new Result<>(200,"此手机号可以注册",null);
    }

    @Override
    public Result<String> sendTst(String phone) {
        Random random = new Random();
        // 生成一个 0 到 999999 之间的随机整数
        int randomNumber = random.nextInt(1000000);
        // 格式化为6位数，不足部分补零
        String format = String.format("%06d", randomNumber);
        Integer integer = Integer.valueOf(format);
        try {
            SendCodeUtil.sendCode(integer,phone);
        } catch (Exception e) {
            return new Result<>(500,"发送短息失败","发送短息失败");
        }
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        forValue.set(phone,integer.toString());
        return new Result<>(200,"发送短信成功","发送短信成功");
    }

    @Override
    public Result<String> repeat(String phone, String code) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(phone);
        if(code.equals(s)){
            redisTemplate.delete(phone);
            return new Result<>(200,"验证码正确","验证码正确");
        }else {
            return new Result<>(5000,"验证码错误","验证码错误");
        }
    }

    @Override
    public Result<String> addUser(AddUserVo addUserVo) {
        FrontUser frontUser = new FrontUser();
        UUID uuid = UUID.randomUUID();
        // 将UUID转换为字符串形式
        String salt = uuid.toString().replace("-", "");
        Md5Hash md5Hash=new Md5Hash(addUserVo.getPassword(),salt,5);
        frontUser.setUserName("微信用户");
        frontUser.setPassword(md5Hash.toString());
        frontUser.setPhone(addUserVo.getPhoneNumber());
        frontUser.setRegisterTime(new Date());
        frontUser.setCreateTime(new Date());
        frontUser.setSalt(salt);
        frontUser.setStatus("0");
        int insert = frontUserDao.insert(frontUser);
        if(insert==0){
            return new Result<>(5000,"注册失败","注册失败");
        }else {
            return new Result<>(200,"注册成功","注册成功");
        }
    }

    @Override
    public Result<String> updatePass(AddUserVo addUserVo) {
        System.out.println(addUserVo.getPassword());
        QueryWrapper<FrontUser> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",addUserVo.getPhoneNumber());
        FrontUser frontUser = frontUserDao.selectOne(wrapper);
        Md5Hash md5Hash=new Md5Hash(addUserVo.getPassword(),frontUser.getSalt(),5);
        frontUser.setPassword(md5Hash.toString());
        frontUserDao.updateById(frontUser);
        return new Result<>(200,"修改成功",null);
    }
}
